﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayPointSprinkler : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIApplication application = commandData.Application;
				UIDocument activeUIDocument = application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
				{
					(ViewType)1,
					(ViewType)2
				}))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					FormLayPointSprinkler formLayPointSprinkler = new FormLayPointSprinkler(document);
					if (formLayPointSprinkler.ShowDialog() == DialogResult.Cancel)
					{
						result = Autodesk.Revit.UI.Result.Failed;
					}
					else
					{
						SprinklersParam sprinklersPara = formLayPointSprinkler.sprinklersPara;
						int sprinklerCategory = formLayPointSprinkler.familyCategory;
						int sprinlkerLocationType = formLayPointSprinkler.sprinlkerLocationType;
						FamilySymbol familySymbol = null;
						if (formLayPointSprinkler.CurrentBtn == 1)
						{
							ConnectHelper connectHelper = new ConnectHelper();
							transaction = new Transaction(document, "replace");
							transaction.Start();
							familySymbol = formLayPointSprinkler.Fsymbol;
							IEnumerable source = connectHelper.LayFamilyInstance(new List<XYZ>
							{
								new XYZ(1.0, 1.0, 1.0)
							}, familySymbol, document.ActiveView.GenLevel, 0, document);
							document.Regenerate();
							FamilyInstance familyInstance = (from Element ele in source
							where ele != null
							select ele as FamilyInstance).FirstOrDefault<FamilyInstance>();
							if (familyInstance != null)
							{
								Connector elementConnector = YJKRevitTools.GetElementConnector(familyInstance);
								if (elementConnector == null)
								{
									return Autodesk.Revit.UI.Result.Cancelled;
								}
								if (elementConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.001))
								{
									sprinklerCategory = 2;
								}
								else
								{
									if (!elementConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0), 0.001))
									{
										YJKMessageBox.Information("选择的喷头不符合盈建科制族规范，不能进行喷头替换");
										return Autodesk.Revit.UI.Result.Cancelled;
									}
									sprinklerCategory = 1;
								}
								document.DeleteElement(familyInstance);
							}
							transaction.Commit();
						}
						for (;;)
						{
							transaction = new Transaction(document, "即点加喷头");
							transaction.Start();
							if (!new LaySprinklerHelper(application, activeUIDocument, ref message, sprinklersPara, sprinklerCategory, sprinlkerLocationType).LayPointSprinkler(familySymbol, formLayPointSprinkler.CurrentBtn))
							{
								break;
							}
							transaction.Commit();
						}
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && transaction.GetStatus() != (TransactionStatus)3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
